const app = getApp();

function getPasswords(event) {
  const that = this;
  let passwords = [];
  for (let i = 0; i < 6; i++) {
    passwords.push({
      id: i + 1,
      value: '',
    })
  }

  that.setData({
    passwords,
    password: "", //输入的连续密码
    posFocus: true, //是否聚焦拉取输入法
    passwordDone: false, //密码6位输入完毕
  })
}

function inputPassword(event) {
  const that = this;
  let value = event.detail.value;
  let passwords = that.data.passwords;
  let password = that.data.password || '';
  //6位密码输入完毕后是否需要重置
  let noResetPass = that.data.noResetPass || false;
  let setdata = {};

  // 防止用户删除的不是最后一个数
  if (password.length > value.length) {
    value = password.slice(0, password.length - 1);
  }

  for (let i = 0; i < passwords.length; i++) {
    passwords[i].value = value.slice(i, i + 1);
  }

  setdata.passwords = passwords;
  setdata.password = value;

  if (value.length == 6) {
    setdata.posFocus = false;
    setdata.passwordDone = true;
    // 输完6位重置
    if (!noResetPass){
      passwords.forEach(item => item.value = "");
      setdata.passwords = passwords;
      setdata.password = "";
    }
    that.setData(setdata);
    that.doneInputPassword(value);
  } else {
    setdata.passwordDone = false;
    that.setData(setdata);
  }
}

export default {
  getPasswords, //获取密码数组格式
  inputPassword, //监听用户输入框输入值
};